/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Oct 9, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits.file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {
    /**
     *
     * @param folder       folder under which the search is done
     * @param ext          ignore case. recommend to include "." .
     * @param findFistone  true if only need find first one
     * @return             A list contain File object
     */
    public static List getFilesByExtension(File folder, String ext,boolean findFistone){
        if(folder==null||ext==null){
            throw new NullPointerException("null passed in FileUtils.getFileByExtension");
        }
        if(!folder.isDirectory() ){
            throw new IllegalArgumentException("FileUtils.getFileByExtension must be passed in a directory file");

        }

        File[] files=folder.listFiles();
        ArrayList founds=new ArrayList();
        for (int i=0;i<files.length ;i++){
            if(files[i].isFile() ){
                try {
                    String fileName=files[i].getCanonicalPath() ;
                    String myext=ext.toLowerCase() ;
                    if(fileName.endsWith(myext)){
                        founds.add(files[i]);
                        if(findFistone){
                            return founds;
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use Options | File Templates.
                }
            }
        }
        return founds;
    }
    /**
     * Find first one
     * @param folder
     * @param ext
     * @return
     */
    public static File getFileByExtension(File folder, String ext){
         List fileList=getFilesByExtension(folder,ext,true);
         if(fileList!=null&&fileList.size() >0){
             return (File) fileList.get(0);
         }
         else{
             return null;
         }
    }
     /**
     * No test yet 
     * @param folder       folder under which the search is done
     * @param name          ignore case. include ext. Not include path
     * @param findFistone  true if only need find first one
     * @return             A list contain File object
     */
    public static List getFilesByName(File folder, String name,boolean findFistone){
        if(folder==null||name==null){
            throw new NullPointerException("null passed in FileUtils.getFileByExtension");
        }
        if(!folder.isDirectory() ){
            throw new IllegalArgumentException("FileUtils.getFileByExtension must be passed in a directory file");

        }

        File[] files=folder.listFiles();
        ArrayList founds=new ArrayList();
        for (int i=0;i<files.length ;i++){
            if(files[i].isFile() ){

                    String fileName=files[i].getName() ;

                    if(fileName.equalsIgnoreCase(name) ){
                        founds.add(files[i]);
                        if(findFistone){
                            return founds;
                        }
                    }

            }
        }
        return founds;
    }
    /**
     * Find first one
     * @param folder
     * @param ext
     * @return
     */
    public static File getFileByName(File folder, String ext){
         return  (File) getFilesByExtension(folder,ext,true);
    }

    public static String getUserHome(){
        String userHome=System.getProperty("user.home");
        String userHomePath=userHome.replace('\\','/');
        return   userHomePath;
    }
}
